<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="维修状态" prop="repairType">
            <el-select v-model="queryParams.repairType" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
              <el-option label="已维修" value="1"></el-option>
              <el-option label="待维修" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="损坏状态" prop="repairStatus">
            <el-select v-model="queryParams.repairStatus" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
              <el-option v-for="dict in repairStatusOptions" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="维修时间">
            <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="资产名称" align="center" prop="assetsName" />
      <el-table-column label="资产ID" align="center" prop="assetsIdCard" />
      <el-table-column label="报修人" align="center" prop="reportUserName" />
      <el-table-column label="报修时间" align="center" prop="reportTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报修描述" align="center" prop="reportDesc" />
      <el-table-column label="维修人" align="center" prop="repairUserName" />
      <el-table-column label="损坏状态" align="center" prop="repairStatus">
        <template slot-scope="scope">
          <dict-tag :options="repairStatusOptions" :value="scope.row.repairStatus" />
        </template>
      </el-table-column>
      <el-table-column label="维修时间" align="center" prop="repairTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修描述" align="center" prop="repairDesc" />
      <el-table-column label="配件金额" align="center" prop="fittingMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.fittingMoney) }}
        </template>
      </el-table-column>
      <el-table-column label="其他金额" align="center" prop="repairMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.repairMoney) }}
        </template>
      </el-table-column>
      <el-table-column label="维修合计" align="center">
        <template slot-scope="scope">
          {{ toMoney((scope.row.fittingMoney ? scope.row.fittingMoney : 0) + (scope.row.repairMoney ?
          scope.row.repairMoney : 0)) }}
        </template>
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="repairType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.repairType == 1">已维修</el-tag>
          <el-tag type="danger" v-if="scope.row.repairType == 0">待维修</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.repairType == 0"
            @click="handleConfirmRepairOpen(scope.row, 1)" v-hasPermi="['cld:repair:confirm']">确认维修</el-button>
          <el-button size="mini" type="text" @click="handleConfirmRepairOpen(scope.row, 2)"
            v-hasPermi="['cld:repair:query']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 查看 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <view-repair :form="form"></view-repair>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="type == 1" @click="handleConfirmRepairSubmit">确认维修</el-button>
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRepairDetailsList, addPlacementRecord, getRepairDetails } from "@/api/cld/record";
import { repairStatusOptions } from '@/assets/staticDict'
import viewRepair from "./viewRepair.vue";
export default {
  name: "repair",
  dicts: ['cld_assets_type', 'cld_project_region'],
  components: {
    viewRepair
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      open: false,
      title: '',
      // 资产维修明细表格数据
      list: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairStatus: null,
        repairType: null,
        assetsDetailsId: this.$route.params.assetsDetailsId
      },
      // 损坏状态
      repairStatusOptions: repairStatusOptions,
      form: {
        reportImgs: ''
      },
      type: 1
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产维修明细列表 */
    getList() {
      this.loading = true;
      getRepairDetailsList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 确认维修
    handleConfirmRepairOpen(row, type) {
      this.type = type;
      getRepairDetails(row.assetsRecordId).then(res => {
        this.form = res.data;
        this.form.reportImgsList = this.form.repairImgs ? this.form.repairImgs.split(',') : [];
        this.form.repairImgsList = this.form.repairImgs ? this.form.repairImgs.split(',') : [];
        this.title = this.form.assetsName + '-' + this.form.assetsIdCard + '-维修明细';
        this.open = true;
      })
    },
    handleConfirmRepairSubmit() {
      let form = {
        ...this.form,
        detailsList: []
      }
      addPlacementRecord(form).then(res => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      })
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
